package com.hjsmallfly.syllabus;

import com.hjsmallfly.syllabus.di.qualifier.retrofitQualifier.*;
import com.readystatesoftware.chuck.ChuckInterceptor;
import dagger.Module;
import dagger.Provides;
import okhttp3.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

import javax.inject.Singleton;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * Created by daidaijie on 2016/10/8.
 */

@Module
public class RetrofitModule {

    @SchoolRetrofit
    @Provides
    @Singleton
    public Retrofit provideSchoolRetrofit() {
        return new Retrofit.Builder()
                .client(provideOkHttpClient().build())
                .baseUrl("https://class.stuapps.com")
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @BmobRetrofit
    @Provides
    @Singleton
    public Retrofit provideBmobRetrofit() {
        return new Retrofit.Builder()
                .client(provideOkHttpClient().build())
                .baseUrl("https://api.bmob.cn/1/")
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @OARetrofit
    @Provides
    @Singleton
    public Retrofit provideOARetrofit() {
        return new Retrofit.Builder()
                .baseUrl("http://wechat.stu.edu.cn/webservice_oa/oa_stu_/")
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(provideOkHttpClient().build())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @LibraryRetrofit
    @Provides
    @Singleton
    public Retrofit provideLibraryRetrofit() {
        return new Retrofit.Builder()
                .baseUrl("http://opac.lib.stu.edu.cn:83/opac/")
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
    }


    @GieeRetrofit
    @Provides
    @Singleton
    public Retrofit provideGiteeRetrofit() {
        return new Retrofit.Builder()
                .baseUrl("https://gitee.com/")
                .client(provideOkHttpClient().build())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
    }

    private static OkHttpClient.Builder provideOkHttpClient() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        // 设置连接超时时间, Retrofit库默认都是10s
        builder.readTimeout(30, TimeUnit.SECONDS);
        builder.connectTimeout(20, TimeUnit.SECONDS);
        builder.writeTimeout(30, TimeUnit.SECONDS);
        builder.addInterceptor(new ChuckInterceptor(App.getContext()));
        return builder;
    }

    private static Interceptor proviceFixInterceptor() {
        return new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request();
                Response originalResponse = chain.proceed(request);
                if (originalResponse.body().contentType().toString().equals("application/json")) {
                    String json = originalResponse.body().toString();
                    String response = json;
                    try {
                        JSONObject jsonObj = new JSONObject(json);
                        Object data = jsonObj.get("data");
                        if (data instanceof JSONArray) {
                            jsonObj.remove("data");
                            JSONObject list = new JSONObject();
                            list.put("lists", data);
                            jsonObj.put("data", list);
                            response = jsonObj.toString();
                        }
                        return originalResponse.newBuilder().body(
                                ResponseBody.create(originalResponse.body().contentType(), response)
                        ).build();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                return originalResponse;
            }
        };
    }
}
